package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.BasicParkEntity;
import com.iwomy.envpro.platform.entity.BasicTransmissionMethodEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.BasicTransmissionMethodService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基础信息-数据传输方式
 *
 * @author pig
 * @date 2023-09-23 15:47:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicTransmissionMethod" )
@Tag(description = "basicTransmissionMethod" , name = "基础信息-数据传输方式管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicTransmissionMethodController {

    private final  BasicTransmissionMethodService basicTransmissionMethodService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param basicTransmissionMethod 基础信息-数据传输方式
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_view')" )
    public R getBasicTransmissionMethodPage(@ParameterObject Page page,@ParameterObject BasicTransmissionMethodEntity basicTransmissionMethod) {
        LambdaQueryWrapper<BasicTransmissionMethodEntity> wrapper = Wrappers.lambdaQuery(basicTransmissionMethod);
        return R.ok(basicTransmissionMethodService.page(page, wrapper));
    }


    /**
     * 通过id查询基础信息-数据传输方式
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicTransmissionMethodService.getById(id));
    }

    /**
     * 新增基础信息-数据传输方式
     * @param basicTransmissionMethod 基础信息-数据传输方式
     * @return R
     */
    @Operation(summary = "新增基础信息-数据传输方式" , description = "新增基础信息-数据传输方式" )
    @SysLog("新增基础信息-数据传输方式" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_add')" )
    public R save(@RequestBody BasicTransmissionMethodEntity basicTransmissionMethod) {
		BasicTransmissionMethodEntity wrapperEntity = new BasicTransmissionMethodEntity();
		wrapperEntity.setTransmissionMethod(basicTransmissionMethod.getTransmissionMethod());
		LambdaQueryWrapper<BasicTransmissionMethodEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicTransmissionMethodEntity one = basicTransmissionMethodService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
			throw new PlatformException("数据传输方式已存在");
		}
        return R.ok(basicTransmissionMethodService.save(basicTransmissionMethod));
    }

    /**
     * 修改基础信息-数据传输方式
     * @param basicTransmissionMethod 基础信息-数据传输方式
     * @return R
     */
    @Operation(summary = "修改基础信息-数据传输方式" , description = "修改基础信息-数据传输方式" )
    @SysLog("修改基础信息-数据传输方式" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_edit')" )
    public R updateById(@RequestBody BasicTransmissionMethodEntity basicTransmissionMethod) {
		BasicTransmissionMethodEntity wrapperEntity = new BasicTransmissionMethodEntity();
		wrapperEntity.setTransmissionMethod(basicTransmissionMethod.getTransmissionMethod());
		LambdaQueryWrapper<BasicTransmissionMethodEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicTransmissionMethodEntity one = basicTransmissionMethodService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)&&!one.getId().equals(basicTransmissionMethod.getId())){
			throw new PlatformException("数据传输方式已存在");
		}
        return R.ok(basicTransmissionMethodService.updateById(basicTransmissionMethod));
    }

    /**
     * 通过id删除基础信息-数据传输方式
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除基础信息-数据传输方式" , description = "通过id删除基础信息-数据传输方式" )
    @SysLog("通过id删除基础信息-数据传输方式" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(basicTransmissionMethodService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicTransmissionMethod 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicTransmissionMethod_export')" )
    public List<BasicTransmissionMethodEntity> export(BasicTransmissionMethodEntity basicTransmissionMethod) {
        return basicTransmissionMethodService.list(Wrappers.query(basicTransmissionMethod));
    }
}